package com.cskaoyan.catmall.controller.background;


import com.cskaoyan.catmall.model.bo.MarketExpressBo;
import com.cskaoyan.catmall.model.bo.MarketMallBo;
import com.cskaoyan.catmall.model.bo.MarketOrderBo;
import com.cskaoyan.catmall.model.bo.MarketWxBo;
import com.cskaoyan.catmall.model.vo.*;
import com.cskaoyan.catmall.service.ConfigService;
import com.cskaoyan.catmall.utils.ValidatedUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 创建日期: 2022-01-07-09-13
 * author : XGQ
 * description: 配置管理
 */

@RestController
@RequestMapping("admin/config")
public class ConfigController {

    @Autowired
    ConfigService configService;

    /**
     * @description 商场配置页面显式
     * @author XGQ
     * @date 2022/01/07 14:55
     * @param
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo<com.cskaoyan.catmall.model.vo.MarketMallVo>
     */
    @GetMapping("mall")
    public BaseRespVo<MarketMallVo> getMallConfig() {

        //从数据库获取参数
        String longutude = configService.getValue("market_mall_longitude");
        String latitude = configService.getValue("market_mall_latitude");
        String address = configService.getValue("market_mall_address");
        String qq = configService.getValue("market_mall_qq");
        String phone = configService.getValue("market_mall_phone");
        String name = configService.getValue("market_mall_name");

        //响应参数
        MarketMallVo marketMallVo = new MarketMallVo();
        marketMallVo.setMarket_mall_longitude(longutude);
        marketMallVo.setMarket_mall_latitude(latitude);
        marketMallVo.setMarket_mall_address(address);
        marketMallVo.setMarket_mall_qq(qq);
        marketMallVo.setMarket_mall_phone(phone);
        marketMallVo.setMarket_mall_name(name);
        return BaseRespVo.ok(marketMallVo);
    }

    /**
     * @description 商场配置更新
     * @author XGQ
     * @date 2022/01/07 16:03
     * @param
     * @param marketMallBo
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo
     */
    @PostMapping("mall")
    public BaseRespVo postMallConfig(@RequestBody @Validated MarketMallBo marketMallBo,BindingResult bindingResult) {
        if (bindingResult.hasFieldErrors()) {
            return ValidatedUtil.validatedResult(bindingResult);
        }

        //获取页面参数
        String longutude = marketMallBo.getMarket_mall_longitude();
        String latitude = marketMallBo.getMarket_mall_latitude();
        String address = marketMallBo.getMarket_mall_address();
        String qq = marketMallBo.getMarket_mall_qq();
        String phone = marketMallBo.getMarket_mall_phone();
        String name = marketMallBo.getMarket_mall_name();

        //存入数据库，返回key_name,key_value
        configService.updateByKeyValue("market_mall_longitude", longutude);
        configService.updateByKeyValue("market_mall_latitude", latitude);
        configService.updateByKeyValue("market_mall_address", address);
        configService.updateByKeyValue("market_mall_qq", qq);
        configService.updateByKeyValue("market_mall_phone", phone);
        configService.updateByKeyValue("market_mall_name", name);
        return BaseRespVo.ok();
    }

    /**
     * @description 运费配置页面显式
     * @author XGQ
     * @date 2022/01/07 16:19
     * @param
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo<com.cskaoyan.catmall.model.vo.MarketExpressVo>
     */
    @GetMapping("express")
    public BaseRespVo<MarketExpressVo> getExpressConfig() {
        //从数据库获取参数
        String freightMin = configService.getValue("market_express_freight_min");
        String freightValue = configService.getValue("market_express_freight_value");

        //响应参数
        MarketExpressVo marketExpressVo = new MarketExpressVo();
        marketExpressVo.setMarket_express_freight_min(freightMin);
        marketExpressVo.setMarket_express_freight_value(freightValue);
        return BaseRespVo.ok(marketExpressVo);
    }

    /**
     * @description 更新运费配置数据
     * @author XGQ
     * @date 2022/01/07 16:26
     * @param
     * @param marketExpressBo
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo
     */
    @PostMapping("express")
    public BaseRespVo postExpressConfig(@RequestBody @Validated MarketExpressBo marketExpressBo, BindingResult bindingResult) {
        if (bindingResult.hasFieldErrors()) {
            return ValidatedUtil.validatedResult(bindingResult);
        }
        //获取页面参数
        String freightMin = marketExpressBo.getMarket_express_freight_min();
        String freightValue = marketExpressBo.getMarket_express_freight_value();

        //响应参数
        configService.updateByKeyValue("market_express_freight_min", freightMin);
        configService.updateByKeyValue("market_express_freight_value", freightValue);
        return BaseRespVo.ok();
    }


    /**
     * @description 订单配置页面显示
     * @author XGQ
     * @date 2022/01/07 16:41
     * @param
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo<com.cskaoyan.catmall.model.vo.MarketOrderVo>
     */
    @GetMapping("order")
    public BaseRespVo<MarketOrderVo> getOrderConfig() {
        //从数据库获取参数
        String unconfirm = configService.getValue("market_order_unconfirm");
        String unpaid = configService.getValue("market_order_unpaid");
        String comment = configService.getValue("market_order_comment");

        //响应参数
        MarketOrderVo marketOrderVo = new MarketOrderVo();
        marketOrderVo.setMarket_order_unconfirm(unconfirm);
        marketOrderVo.setMarket_order_unpaid(unpaid);
        marketOrderVo.setMarket_order_comment(comment);
        return BaseRespVo.ok(marketOrderVo);
    }


    /**
     * @description 更新订单配置数据
     * @author XGQ
     * @date 2022/01/07 16:36
     * @param
     * @param marketOrderBo
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo
     */
    @PostMapping("order")
    public BaseRespVo postOrderConfig(@RequestBody @Validated MarketOrderBo marketOrderBo,BindingResult bindingResult) {
        if (bindingResult.hasFieldErrors()) {
            return ValidatedUtil.validatedResult(bindingResult);
        }
        //获取页面参数
        String unconfirm = marketOrderBo.getMarket_order_unconfirm();
        String unpaid = marketOrderBo.getMarket_order_unpaid();
        String comment = marketOrderBo.getMarket_order_comment();

        //响应参数
        configService.updateByKeyValue("market_order_unconfirm", unconfirm);
        configService.updateByKeyValue("market_order_unpaid", unpaid);
        configService.updateByKeyValue("market_order_comment", comment);
        return BaseRespVo.ok();
    }

    /**
     * @description 小程序配置页面显示
     * @author XGQ
     * @date 2022/01/07 16:48
     * @param
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo<com.cskaoyan.catmall.model.vo.MarketWxVo>
     */
    @GetMapping("wx")
    public BaseRespVo<MarketWxVo> getWxConfig() {
//        从数据库获取参数
        String indexNew = configService.getValue("market_wx_index_new");
        String indexTopic = configService.getValue("market_wx_index_topic");
        String share = configService.getValue("market_wx_share");
        String indexBrand = configService.getValue("market_wx_index_brand");
        String catlogGoods = configService.getValue("market_wx_catlog_goods");
        String catlogList = configService.getValue("market_wx_catlog_list");
        String indexHot = configService.getValue("market_wx_index_hot");

        //响应参数
        MarketWxVo marketWxVo = new MarketWxVo();
        marketWxVo.setMarket_wx_index_new(indexNew);
        marketWxVo.setMarket_wx_index_topic(indexTopic);
        marketWxVo.setMarket_wx_share(share);
        marketWxVo.setMarket_wx_index_brand(indexBrand);
        marketWxVo.setMarket_wx_catlog_goods(catlogGoods);
        marketWxVo.setMarket_wx_catlog_list(catlogList);
        marketWxVo.setMarket_wx_index_hot(indexHot);
        return BaseRespVo.ok(marketWxVo);
    }

    /**
     * @description 更新小程序配置参数
     * @author XGQ
     * @date 2022/01/07 16:58
     * @param
     * @param marketWxBo
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo
     */
    @PostMapping("wx")
    public BaseRespVo postWxConfig(@RequestBody @Validated MarketWxBo marketWxBo,BindingResult bindingResult) {
        if (bindingResult.hasFieldErrors()) {
            return ValidatedUtil.validatedResult(bindingResult);
        }
        //获取页面参数
        String indexNew = marketWxBo.getMarket_wx_index_new();
        String indexTopic = marketWxBo.getMarket_wx_index_topic();
        String share = marketWxBo.getMarket_wx_share();
        String indexBrand = marketWxBo.getMarket_wx_index_brand();
        String catlogGoods = marketWxBo.getMarket_wx_catlog_goods();
        String catlogList = marketWxBo.getMarket_wx_catlog_list();
        String indexHot = marketWxBo.getMarket_wx_index_hot();

        //响应参数
        configService.updateByKeyValue("market_wx_index_new", indexNew);
        configService.updateByKeyValue("market_wx_index_topic", indexTopic);
        configService.updateByKeyValue("market_wx_share", share);
        configService.updateByKeyValue("market_wx_index_brand", indexBrand);
        configService.updateByKeyValue("market_wx_catlog_goods", catlogGoods);
        configService.updateByKeyValue("market_wx_catlog_list", catlogList);
        configService.updateByKeyValue("market_wx_index_hot", indexHot);
        return BaseRespVo.ok();
    }
}
